package com.piotrs10.bsk.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.piotrs10.bsk.model.Pracownik;
import com.piotrs10.bsk.model.Salon;
import com.piotrs10.bsk.model.Samochod;

public class PracownikDao extends BaseDao {
	public Pracownik get(int id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		Pracownik pracownik = null;
		try {
			statement = connection.prepareStatement("SELECT imie, nazwisko, pensja, salon_id" +
					" FROM pracownik WHERE id = ?;");
			statement.setInt(1, id);
			ResultSet set  = statement.executeQuery();
			set.next();
			pracownik = new Pracownik(id, set.getString(1), set.getString(2), set.getFloat(3),
					set.getInt(4));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return pracownik;
	}
	
	public void delete(Integer id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement("DELETE FROM Pracownik " +
						"WHERE id = ?");
			statement.setInt(1, id);
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public ArrayList<Pracownik> getAll() {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		ArrayList<Pracownik> list = null;
		try {
			statement = connection.prepareStatement("SELECT id, imie, nazwisko, pensja, salon_id" +
					" FROM pracownik;");
			ResultSet set = statement.executeQuery();
			list = new ArrayList<Pracownik>();
			while(set.next()) {
				list.add(new Pracownik(set.getInt(1), set.getString(2), set.getString(3), set.getFloat(4),
						set.getInt(5)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	public void save(boolean insert, Pracownik pracownik) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			if(insert) {
				statement = connection.prepareStatement("INSERT INTO Pracownik " +
						"(imie, nazwisko, pensja, salon_id) VALUES " +
						"(?, ?, ?, ?)");
			} else {
				statement = connection.prepareStatement(
						"UPDATE Samochod SET imie = ? , nazwisko = ?, pensja = ?, salon_id = ?, "
						+ " WHERE id = ?");
			}
			statement.setString(1, pracownik.getImie());
			statement.setString(2, pracownik.getNazwisko());
			statement.setFloat(3, pracownik.getPensja());
			statement.setInt(4, pracownik.getSalon());
			if(!insert)
				statement.setInt(5, pracownik.getId());
			
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
